//
//  LoginViewController.m
//  FD
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "LoginViewController.h"
#import "FDTabBarController.h"
#import "ForgetPwdViewController.h" //找回密码
#import "NewRegisterViewController.h" //注册
#import "LoginAndRegisterController.h" //登录注册 xib
#import "FDNavigationViewController.h" //2019-1-17
//tool
#import "FDUserTool.h"
#import "FDPushWebSocketTool.h"
//view
#import "FDSameToAndroidTextFieldCustomView.h"

@interface LoginViewController ()

/**0.手机号*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *phoneTFView;
/**1.密码*/
@property (nonatomic, strong) FDSameToAndroidTextFieldCustomView *pwdTFView;
/***2.登录*/
@property (nonatomic, strong) UIButton *loginBtn;
/**3.左边注册按钮👈*/
@property (nonatomic, strong) UIButton *forgetPwdBtn; //忘记密码
/**4.右边忘记密码按钮👉*/
@property (nonatomic, strong) UIButton *registerBtn;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setupNav];

    [self setupUI];

    //国际化
    [self internationalization];

    
    self.pwdTFView.tf.secureTextEntry = YES; //默认隐藏
    
//2019-2-12 外网账号密码都是6个8
    self.phoneTFView.tf.text = @"3465893302@qq.com";
    self.pwdTFView.tf.text   = @"888888";
    
//2019-2-18 iPhone5 适配问题 iOS10.3.3
    
//2019-2-20
//    self.phoneTFView.tf.text = @"spring@hkhl.cn";
//    self.pwdTFView.tf.text   = @"1234";
}

- (void)setupNav {
    UIImage *arrowImg = [UIImage imageNamed:@"返回箭头"];
    NSString *loginStr = NSLocalizedString(Login, @"登录");
    
    self.navigationItem.title = loginStr;
    
    //12-29 >=2 显示 左侧返回按钮
//    if (self.navigationController.viewControllers.count>1) {
//        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:arrowImg style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction:)];
//    }
    
    

    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithImage:arrowImg style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction:)];
    barItem.tintColor = [UIColor blackColor];//黑色
    
    self.navigationItem.leftBarButtonItem = barItem;
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.phoneTFView];
    [self.view addSubview:self.pwdTFView];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.registerBtn];
    [self.view addSubview:self.forgetPwdBtn];
    
    [self.phoneTFView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(180);
        make.height.mas_equalTo(50);
    }];
    [self.pwdTFView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.phoneTFView.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.pwdTFView.mas_bottom).offset(30);
    }];
 
    //注册按钮&忘记密码按钮
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.loginBtn.mas_left);
        make.top.equalTo(self.loginBtn.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
        make.width.mas_greaterThanOrEqualTo(100);
    }];
    [self.forgetPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.loginBtn.mas_right);
        make.top.equalTo(self.registerBtn.mas_top);
        make.height.mas_equalTo(40);
        make.width.mas_greaterThanOrEqualTo(100);
    }];
}



#pragma mark  - getter
- (FDSameToAndroidTextFieldCustomView *)phoneTFView {
    if (!_phoneTFView) {
        NSString *zhStr = NSLocalizedString(Put_your_email, @"请输入账号");
        _phoneTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) title:zhStr];
    }
    return _phoneTFView;
}
- (FDSameToAndroidTextFieldCustomView *)pwdTFView {
    if (!_pwdTFView) {
        _pwdTFView = [[FDSameToAndroidTextFieldCustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50) title:NSLocalizedString(Put_your_pwd, @"请输入密码")];
        _pwdTFView.tf.secureTextEntry = YES; //密文
        
        //睁眼闭眼按钮
        UIButton *yanjinBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [yanjinBtn setImage:[UIImage imageNamed:@"by"] forState:(UIControlStateNormal)];
        [yanjinBtn setImage:[UIImage imageNamed:@"zy"] forState:(UIControlStateSelected)];
        [yanjinBtn addTarget:self action:@selector(clickYanJinBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_pwdTFView.topBgView addSubview:yanjinBtn];
        [yanjinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self->_pwdTFView.topBgView);
            make.width.mas_equalTo(30);
        }];
    }
    
    return _pwdTFView;
}


- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _loginBtn.backgroundColor = kMainRedColor;
        [_loginBtn setTitle:@"登录" forState:(UIControlStateNormal)];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [_loginBtn addTarget:self action:@selector(clicklLoginAction:) forControlEvents:(UIControlEventTouchUpInside)];
        //弧度4
        _loginBtn.layer.masksToBounds = YES;
        _loginBtn.layer.cornerRadius = 4;
    }
    return _loginBtn;
}

//👈注册
- (UIButton *)registerBtn {
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_registerBtn setTitle:NSLocalizedString(Register, @"注册") forState:(UIControlStateNormal)];
        [_registerBtn setTitleColor:kLightGrayTextColor forState:(UIControlStateNormal)];
        [_registerBtn addTarget:self action:@selector(clickRegisterBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        
        //文字左对齐
        _registerBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    }
    return _registerBtn;
}

//忘记密码
- (UIButton *)forgetPwdBtn {
    if (!_forgetPwdBtn) {
        _forgetPwdBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_forgetPwdBtn setTitle:@"忘记密码?" forState:(UIControlStateNormal)];
        [_forgetPwdBtn setTitleColor:kLightGrayTextColor forState:(UIControlStateNormal)];
        _forgetPwdBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_forgetPwdBtn addTarget:self action:@selector(clickFrogetPwdBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        //文字右对齐
        _forgetPwdBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
    return _forgetPwdBtn;
}


#pragma mark  - 国际化
- (void)internationalization {
//    self.phoneTF.placeholder = NSLocalizedString(Put_your_phone, @"请输入手机号");
//    self.pwdTF.placeholder = NSLocalizedString(Put_your_pwd, @"请输入密码");
    NSString *loginStr = NSLocalizedString(Login, @"登录");
    [self.loginBtn setTitle:loginStr forState:(UIControlStateNormal)];
    
    NSString *forgetpwdStr = NSLocalizedString(ForgetPassword, @"忘记密码");
    NSString *forgetpwdStrHaveQuestionMarkStr = [NSString stringWithFormat:@"%@?",forgetpwdStr];
    [self.forgetPwdBtn setTitle:forgetpwdStrHaveQuestionMarkStr forState:(UIControlStateNormal)];
}


//internationalization/
#pragma mark  - 事件监听
- (void)backAction:(UIBarButtonItem *)barItem {
    FDLog(@"点击了返回");
    [self dismissViewControllerAnimated:YES completion:nil];

    if (self.isKickOffByUNAUTHORIZED) {
        FDLog(@"2019-2-25是否因为接口请求 UNAUTHORIZED才被踢到这个页面");
        //切换根视图控制器
        [[FDTabBarController shareFDTabarVC] setSelectedIndex:0];
        [UIApplication sharedApplication].keyWindow.rootViewController = [FDTabBarController shareFDTabarVC];
        [[FDTabBarController shareFDTabarVC] setSelectedIndex:0];
    }
}




//!!!!:眼睛
- (void)clickYanJinBtnAction:(UIButton *)btn {
    if (btn.selected) {
        self.pwdTFView.tf.secureTextEntry = YES;
    }else {
        self.pwdTFView.tf.secureTextEntry = NO;
    }
    
    //代码顺序:放最后面
    btn.selected = !btn.selected;
}
//点击登录
- (void)clicklLoginAction:(UIButton *)button {
    [self.view endEditing:YES]; //回收键盘
    
    NSString *phone = self.phoneTFView.tf.text;
    NSString *pwd = self.pwdTFView.tf.text;
    
    if ([NSString isEmptyString:phone]) {
        [JRToast showWithText:NSLocalizedString(ToastPutAccount, @"请输入账号")];
        return;
    }
    
    if ([NSString isEmptyString:pwd]) {
        [JRToast showWithText:NSLocalizedString(Put_your_pwd, @"请输入密码")];
        return;
    }
    
    NSDictionary *parDic = @{@"name":phone,@"password":pwd};
    
    [MBProgressHUD fd_showMessage:@"" toView:self.view]; //菊花
    
    [FDHttpAPi requestLoginWithParDic:parDic successBlock:^(id responseObjc) {
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            
            //保存账号
            [FDUserTool fd_saveUserInfo:parDic];
            
            [self skipToFDTabbarVC]; //请求成功跳转界面
            //单例
            [[FDPushWebSocketTool defaultFdWebPushSockcet] setupAndOpenPushWebSocket];
        }else {
            [JRToast showWithText:responseObjc[describe]];//请求失败
        }
        
        [MBProgressHUD fd_hideHUDFromView:self.view]; //移除菊花
    } failureBlock:^(NSError *error) {
        FDLog(@"%@",[error localizedDescription]);
        [MBProgressHUD fd_hideHUDFromView:self.view];
    }];
}


//点击了注册
- (void)clickRegisterBtnAction:(UIButton *)button {
    FDLog(@"点击了注册");
    
    [self.view endEditing:YES];
    
    NewRegisterViewController *registerVC  = [[NewRegisterViewController alloc] init];
    UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:registerVC];
    [self presentViewController:navVC animated:YES completion:nil];
}

//点击忘记密码
- (void)clickFrogetPwdBtnAction:(UIButton *)button {
    
    [self.view endEditing:YES];
    
    ForgetPwdViewController *findPWdVC = [[ForgetPwdViewController alloc] init];
    
    FDNavigationViewController *navVC = [[FDNavigationViewController alloc] initWithRootViewController:findPWdVC];
    [self presentViewController:navVC animated:YES completion:nil];
}


- (void)skipToFDTabbarVC {
    FDTabBarController *fdtabbarVC = [FDTabBarController shareFDTabarVC];
    fdtabbarVC.selectedIndex = 0;
    [UIApplication sharedApplication].keyWindow.rootViewController = fdtabbarVC;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //回收键盘
    [self.view endEditing:YES];
    [self.view resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 回收键盘



- (void)dealloc
{
    FDLog(@"%@ 挂了",self);
}

@end
